<template>
  <div class="music">
    <div class="music_container">
      <!-- 搜索部分 -->
      <header>
        <input
          type="text"
          autocomplete="off"
          placeholder="请输入歌名"
          autofocus="autofocus"
          class="searchFor"
          @keyup.enter="searchClick($event)"
          :value="texteles"
          ref="text1"
        />
        <button @click="searchClick1($event)">搜索</button>
      </header>
      <!-- 歌曲列表 -->
      <section class="main">
        <ul class="list" v-for="(item , index) in list" :key="index">
          <li class="list_music">
            <div class="view">
              <span>{{index+1}}.</span>
              <label>{{item.name}}</label>
              <button @click="play(item.id)"></button>
            </div>
          </li>
        </ul>
      </section>
      <!-- 底部 -->
      <footer class="footer">
        <audio :src="url" controls autoplay class="audios"></audio>
      </footer>
    </div>
  </div>
</template>

<script>
import axios from "axios";
export default {
  /*
        接口歌曲搜索
        https://autumnfish.cn/search
        方法:get
        参数:keywords 搜索关键词
        接口获取歌曲播放地址
        https://autumnfish.cn/song/url
        方法get
        参数:id 歌曲：id
    */

  data() {
    return {
      texteles: "",
      list: null,
      url: ""
    };
  },
  methods: {
    searchClick(event) {
      if (!event.target.value.trim()) {
        return alert("请不要输入空白内容!!!!");
      }
      this.texteles = event.target.value;
      axios({
        url: "https://autumnfish.cn/search",
        method: "get",
        params: {
          keywords: this.texteles
        }
      }).then(res => {
        const data = res.data.result.songs; // eslint-disable-line no-unused-vars
        this.list = data;
      });
    },
    searchClick1() {
      if (!this.$refs.text1.value.trim()) {
        return alert("请不要输入空白内容!!!!");
      }
      axios({
        url: "https://autumnfish.cn/search",
        method: "get",
        params: {
          keywords: this.$refs.text1.value
        }
      }).then(res => {
        const data = res.data.result.songs; // eslint-disable-line no-unused-vars
        this.list = data;
      });
    },
    play(id) {
      axios({
        url: "https://autumnfish.cn/song/url",
        method: "get",
        params: {
          id: id
        }
      }).then(res => {
        this.url = res.data.data[0].url;
      });
    }
  }
};
</script>

<style>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
body {
  background-color: rgb(197, 181, 181);
}
li {
  list-style-type: none;
}
header {
  position: relative;
}
header button {
  position: absolute;
  top: 0;
  right: 0;
  width: 13.889vw;
  height: 100%;
  border: none;
  background-color: transparent;
  outline: none;
  cursor: pointer;
  font-size: 4vw;
}
.music_container {
  width: 80vw;
  height: 100%;
  margin: auto;
  background-color: rgb(230, 222, 224);
  position: relative;
}
.searchFor {
  width: 100%;
  font-size: 4vw;
  border: 0px;
  padding: 2vw 0vw 2vw 3vw;
}
.main {
  width: 100%;
  height: 40vw;
  overflow-x: hidden;
}
.list_music {
  position: relative;
  width: 100%;
  height: 100%;
}
.view {
  width: 100%;
  height: 8vw;
  display: flex;
  align-items: center;
}
.view span {
  margin: 0 10px;
  font-size: 3vw;
  color: rgb(109, 99, 85);
}
.view label {
  flex: 1;
  font-size: 3vw;
  color: rgb(109, 99, 85);
}
.view button {
  position: absolute;
  width: 100%;
  height: 100%;
  opacity: 0;
  top: 0px;
  left: 0px;
}
.footer {
  position: fixed;
  width: 80vw;
  bottom: 0px;
}
.footer .audios {
  width: 100%;
  border-radius: none;
  outline: none;
  background-color: #fff;
  background-size: cover;
}
</style>